<?php
namespace app\index\controller;
use app\index\controller\Base;
class Index extends Base
{
    public function index()
    {
      //总浏览量
      db('click')->where('id',1)->setInc('click');
      //当日浏览量
      $time = db('click')->where('update_time','> time','today')->max('update_time');
      if($time){
        db('click')->where('id', 3)->setInc('click');
      }else{
        db('click')->where('id', 3)->update(['click'=>1]);
      }
      //当日访客
      $res = db('visitor')->select();
      $remote = $_SERVER['REMOTE_ADDR'];
      if($res){
        $onetime = db('visitor')->where('create_time','> time','today')->max('create_time');
        if($onetime){
          //总访客数
          $tory = implode(db('click')->where('id',2)->field('tory')->find())*1;
          $sum = db('visitor')->count('ip');
          $he = $tory + $sum;
          db('click')->where('id',2)->update(['click'=>$he]);

          //当日访客
          $ip = db('visitor')->where('ip',$remote)->select();
          if(empty($ip)){
            db('visitor')->data(['ip'=>$remote])->insert();
          }
          $sum = db('visitor')->count('ip');
          db('click')->where('id',4)->update(['click'=>$sum]);
        }else{
          //总访客数
          $now = implode(db('click')->where('id',2)->field('click')->find())*1;
          $tor = db('click')->where('id',2)->update(['tory'=>$now]);
          db('click')->where('id',2)->update(['click'=>$now + 1]);

          //当日访客
          db('visitor')->where('id','>',0)->delete();
          db('visitor')->data(['ip'=>$remote])->insert();
          $sum = db('visitor')->count('ip');
          db('click')->where('id',4)->update(['click'=>$sum]);
        }
      }else{
        //当日访客
        db('visitor')->data(['ip'=>$remote])->insert();
        $sum = db('visitor')->count('ip');
        db('click')->where('id',4)->update(['click'=>$sum]);

        //总访客数
        $tory = implode(db('click')->where('id',2)->field('tory')->find())*1;
        $he = $tory + $sum;
        db('click')->where('id',2)->update(['click'=>$he]);
      }
      return view();
    }

}